# -*- python -*-

Import('*')
import os
import glob

use_cp_mode = mi_config.use_copy_install_mode

##### Construct the root.src.tar.gz
##       and something to fix(like copy install mode files fix)
class miSrcMaker(PkgMaker.StepMaker):
    target_list = ['bindir/root.src.tar.gz']
    ROOT = '$tmpdir/root.src'
    steps = ['init_dir',
             'install_src_step1',
             'install_src_pyext',
             'install_configfiles',
             'install_tranlatefiles',
             'install_post',
             'install_fix',
             'mkmisrc',
            ]

    def init_dir(self):
        cmds = ['rm -rf $ROOT',
        'mkdir -p $ROOT']
        return [], cmds

    def install_src_step1(self):
        inst_file = ['bindir/root.src.step1.tar.gz']
        cmds = [self.get_extract_cmd('bindir/root.src.step1.tar.gz', self.ROOT)]
        return inst_file, cmds

    def install_src_pyext(self):
        # Install python extention module for MI, the package have been packed 
        # in the develop mode.
        inst_file = ['bindir/root.src.pyext.tar.gz']
        cmds = [self.get_extract_cmd('bindir/root.src.pyext.tar.gz', self.ROOT)]
        return inst_file, cmds

    def install_configfiles(self):
        configfiles = [('src/magic.installer/mipublic.py',
                        '$ROOT/$pythondir/site-packages/'),

                       ('src/magic.installer/magic.autopart.xml',
                        '$ROOT/usr/share/MagicInstaller/')]
        slist = []
        cmds = []
        for src, dest in configfiles:
            slist.append(src)
            cmds.extend(['mkdir -p %s' % dest,
                         'cp -a %s %s' % (src, dest)])
        return slist, cmds

    def install_tranlatefiles(self):
        slist = []
        cmds = []
        for lang in mi_config.all_linguas:
            inst_file = 'src/magic.installer/po/%s.mo' % lang
            dest_file = '$ROOT/usr/share/locale/%s/LC_MESSAGES/${textdomain}.mo' % lang
            slist.append(inst_file)
            cmds.extend(['mkdir -p %s' % os.path.dirname(dest_file),
                         'cp -a %s %s' % (inst_file, dest_file)])
        return slist, cmds

    def install_post(self):
        env = self.env
        post_script = '$specdir/post_install.sh'
        if os.path.exists(env.subst(post_script)):
            cmds = ['mkdir -pv $ROOT/root && cp %s $ROOT/root' % post_script,
                    'chmod 700 $ROOT/root/post_install.sh']
            return [post_script], cmds
        else:
            return [], []

    def install_fix(self):
        slist = []
        cmds = []
        if use_cp_mode:
            inst_file = 'src/magic.installer/UIxml/takeactions.xml.cpinstmode'
            dest_file = '$ROOT$root_datadir/UIxml/takeactions.xml'
            slist.append(inst_file)
            cmds.extend['mkdir -p %s' % os.path.dirname(dest_file),
                        'cp -a %s %s' % (inst_file, dest_file)]
        return slist, cmds

    def mkmisrc(self):
        #cmds = ['tar c -C $ROOT . | gzip -9 > $TARGET']
        cmds = ['tar czvf $TARGET -C $ROOT .']
        return [], cmds

misrc = miSrcMaker(env)
misrc.make()

if mi_config.develmode:
    # Pack src/magic.installer and src/etc into root.src.step1.tar.gz
    env.Command('bindir/root.src.step1.tar.gz', [env.Alias('root_src'), env.Alias('root.src.etc')],
                #['tar c -C $destdir . | gzip -9 > $TARGET'])
                ['tar czf $TARGET -C $destdir .'])
    # There will compile the src/pyext (python modules for magicinstaller)
    # Do it at develmode ,because we won't compile the python modules 
    # every time (especially at dist mode).
    env.Command('bindir/root.src.pyext.tar.gz', env.Alias('root.pyext'),
                #['tar c -C $pyextdir . | gzip -9 > $TARGET'])
                ['tar czf $TARGET -C $pyextdir .'])
    

### Hotfix file
# Handle with hotfix file
hotfix_path = mi_config.hotfixfiles_dir
if glob.glob(hotfix_path + '/*'):
    Default(env.Command('%s.hotfix' % mi_config.mkisofn(1),
                        DirValue(hotfix_path),
                        ['(cd $SOURCE && find . -print | cpio -o -H newc | gzip -9) > $TARGET']))
else:
    Delete('%s.hotfix' % mi_config.mkisofn(1))

### SConscript
SConscript('src/SConscript')

